package com.itheima.Task;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Set;

@Component
@Slf4j
public class FileCleanTask {
    @Autowired
    private AliOssUtil aliOssUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(cron = "0 0 0 * * ?") // 每天凌晨执行
    public void clean(){
        SetOperations setOperations = redisTemplate.opsForSet();
        Set<String> difference = setOperations.difference("uplord", "save");

        if (!CollectionUtils.isEmpty(difference)){
            for (String url : difference) {
                aliOssUtil.delete(url);
            }
            //清理Redis中的集合
            setOperations.remove("uplord");
            setOperations.remove("save");
            log.info("清理Redis中的集合完成");
        }else {
            log.info("Redis中的集合没有要清理的文件");
        }
    }


}
